package com.lyh.hkcloud.module.common;

import android.app.Activity;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.EditText;
import androidx.annotation.Nullable;
import com.lyh.hkcloud.constants.UrlConstants;
import com.lyh.hkcloud.util.base.JsonUtil;
import com.lyh.hkcloud.util.base.LoadingUtils;
import com.lyh.hkcloud.util.base.StringUtils;
import com.lyh.hkcloud.util.base.ToastUtils;
import com.lyh.hkcloud.util.http.Request;
import com.lyh.hkcloud.util.http.listener.json.JsonFailuredListener;
import com.lyh.hkcloud.util.http.listener.json.JsonSuccessListener;
import org.json.JSONException;
import org.json.JSONObject;

public class ModuleService extends Service {
    protected Activity activity;


    public ModuleService(Activity activity) {
        this.activity = activity;
    }

    public void getNowDate(final JsonSuccessListener successListener) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("format", "yyyy-MM-dd");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Request.post(activity, UrlConstants.BASE_URL + "/eip-mapp-umc-server/date/getServiceDate", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
                } else {
                    successListener.onRespone(msg, response);
                }
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
            }
        });
    }

    public void checkBox(final EditText et) {
        if (et == null || StringUtils.isEmpty(et.getText().toString().trim())) {
            return;
        }
        LoadingUtils.getInstance(activity).show();
        JSONObject jsonObject = JsonUtil.put("boxId", et.getText().toString().trim());
        JsonUtil.put(jsonObject,"boxStatus","");
        Request.post(activity, UrlConstants.BUS_ROOT + "/requestWms/getBox", jsonObject, new JsonSuccessListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                if (!"0000".equals(response.optString("code"))) {
                    ToastUtils.makeText(activity, response.optString("msg"), ToastUtils.LENGTH_SHORT).show();
                    et.setText("");
                    et.requestFocus();
                }
                LoadingUtils.getInstance(activity).hide();
            }
        }, new JsonFailuredListener() {
            @Override
            public void onRespone(String msg, JSONObject response) {
                Log.e("error",response.optString("message"));
                LoadingUtils.getInstance(activity).hide();
                ToastUtils.makeText(activity, msg, ToastUtils.LENGTH_SHORT).show();
                et.requestFocus();
            }
        });
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
}
